package com.study.d015propertybind.fx;

import java.util.Comparator;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.stage.Stage;

/**
*@description SimpleListProperty的使用
*@author xtayhame
*date 2020年3月5日
*version
*since v1.0
*/
public class SimpleListPropertyMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		ObservableList<String> list=FXCollections.observableArrayList();
		list.add("A");
		list.add("B");
		list.add("C");

		SimpleListProperty<String> slp=new SimpleListProperty<String>(list);

		//增加监听
		slp.addListener(new ChangeListener<ObservableList<String>>() {
			@Override
			public void changed(ObservableValue<? extends ObservableList<String>> observable,
					ObservableList<String> oldValue, ObservableList<String> newValue) {
				//这里的oldValue  和 newValue 的值是一样的
				System.out.println("oldValue="+oldValue);
				System.out.println("newValue="+newValue);
			}
		});


		//添加监听
		slp.addListener(new ListChangeListener<String>() {
			@Override
			public void onChanged(javafx.collections.ListChangeListener.Change<? extends String> c) {
				//Invalid Change state: next() must be called before inspecting the Change.
				// next()方法用于调整索引
				//c.reset(); //重置索引
				if(c.next()){
					//新增
					System.out.println("wasAdded="+c.wasAdded());
					//移除
					System.out.println("wasRemoved="+c.wasRemoved());
					//替换
					System.out.println("wasReplaced="+c.wasReplaced());
					//更新
					System.out.println("wasUpdated="+c.wasUpdated());
					//排序
					System.out.println("wasPermutated="+c.wasPermutated());

					System.out.println("获取最新的List:"+c.getList());


				}

			}
		});

		//slp.add("D");
		//slp.remove(0);
		slp.set(0, "F");



		//如果使用slp.sort()进行排序，则wasPermutated的属性是false, 可以使用对list进行排序
		/*slp.sort(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return o2.compareTo(o1);
			}
		});*/


		//wasPermutated=true
		list.sort(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return o2.compareTo(o1);
			}
		});

		System.out.println("排序："+slp.getValue());


		System.out.println("********************集合绑定测试********************");
		//集合的绑定：
		ObservableList<String> s1=FXCollections.observableArrayList();
		s1.addAll("A","B");

		ObservableList<String> s2=FXCollections.observableArrayList();
		s2.addAll("C","D");

		SimpleListProperty<String>  sp1 = new SimpleListProperty<String>(s1);
		SimpleListProperty<String>  sp2 = new SimpleListProperty<String>(s2);
		//会同时向s2中添加元素，维护s2， sp1和sp2中的元素相同, 但是s1和s2的元素不一样
		//sp1.bind(sp2);
		//和 bind()方法效果一样,会同时向s2中添加元素，维护s2， sp1和sp2中的元素相同, 但是s1和s2的元素不一样
		sp1.bindBidirectional(sp2);
		//sp1元素不会影响sp2， 但是sp2的元素会影响sp1
		//sp1.bindContent(sp2);
		//sp1和sp2的元素会相互影响，真正的双向绑定
		//sp1.bindContentBidirectional(sp2);

		sp1.add("E");  s1.add("F");
		sp2.add("G");  s2.add("H");



		System.out.println("sp1="+sp1.getValue() + " |  sp2=" + sp2.getValue());
		System.out.println("s1 =" + s1 + " | s2=" + s2);



		Platform.exit();
	}
}
